"use strict";

var app = require('koa')();
var config = require('./config/index.js');

// ---logger
//app.use(function*(next) {
//  var start = new Date();
//  yield next;
//  var ms = new Date() - start;
//  console.log('%s %s - %s', this.method, this.url, ms);
//});

// ---enable cross domain
var cors = require('koa-cors');//解决跨域
app.use(cors({credentials: true}));
app.use(function*(next) {
  this.set("Access-Control-Allow-Origin", "*");
  this.set("Access-Control-Allow-Methods", "*");
  this.set("Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept");
  if (this.method === 'OPTIONS') {
    this.body = "Access-Control-Allow-Origin";
  } else {
    yield next;
  }
});

// static image
var serve = require('koa-static');
app.use(serve(__dirname + '/static'));
app.use(serve(config.starStatic));


// ---------------router-----------
app.use(require('./router/ip_freq_limit.js'));
app.use(require('./router/params_collector.js'));
app.use(require('./router/token_checker.js'));
app.use(require('./router/log_collector.js'));

//app.use(require('./router/api/v1/test.js'));

app.use(require('./router/api/v1/face_user.js'));
app.use(require('./router/api/v1/face_part.js'));
app.use(require('./router/api/v1/face_render.js'));
//app.use(require('./router/api/v1/face_star.js'));
// manapi
app.use(require('./router/manapi/v1/face_star.js'));
app.use(require('./router/manapi/v1/face_part.js'));
//test token gen
app.use(require('./router/test/face_token.js'));
app.use(require('./router/test/test.js'));
// ---------------router-----------
// 404
app.use(function*() {
  this.status = 404;
  this.body = '~~~404 not found~~~';
});


app.listen(config.httpPort, '0.0.0.0', function () {
  console.log('server listen at ' + config.httpPort);
});